﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Hypermarket_Serial_Port
{
    class CheckInformationManager : PriceTagTaskManager
    { 

        #region Constructor
        public CheckInformationManager()
        {
        }
        #endregion


        #region Operation Check Product Information
        public override List<string> TaskOperation(List<string> receivedInfoList)
        {
           try
            {
                SerialPortLog.Log("Price Tag Start");
                string machineID = receivedInfoList[Constant.ZERO];
                string type = receivedInfoList[Constant.ONE];
                string productID = mDBManager.GetActivedPriceTagProductID(machineID);
                SerialPortLog.LogOperation("Price Tag: " + machineID);
                SerialPortLog.LogOperation("Barcode: " + productID);
                return CheckValidProductInfo(productID, machineID, type);    
            }
            catch
            {
                SerialPortLog.LogOperation("Price Tag Error");
                SerialPortLog.Log("Price Tag Error");
                throw;
            }
        }
        #endregion 


        #region Check Valid Product Information (Discount/NoDiscount)
        private List<string> CheckValidProductInfo(string productID, string machineID, string type)
        {
            DataTable StockProductDiscount =  mDBManager.getTable(Constant.STOCK_PRODUCT_DISCOUNT_TABLE);
            string query = Constant.PRODUCT_ID + Constant.EQUAL + Constant.SINGAL_QUOTE + productID +
                    Constant.SINGAL_QUOTE + Constant.AND + Constant.EXPIRE_DATE + Constant.MORE_THAN +
                    Constant.SINGAL_QUOTE + DateTime.Today + Constant.SINGAL_QUOTE;
            DataRow[] discountRows = StockProductDiscount.Select(query);
            if (discountRows.Count() == Constant.MIN_ROW_COUNT)
            {
                //select from stock table only, no discount for this product
                return NoDiscount(productID, machineID, type);
            }
            else
            {
                return HaveDiscount(productID, discountRows.First(), machineID, type);
            }
        }

        private List<string> NoDiscount(string productID, string machineID, string type)
        {
            DataTable Stock = mDBManager.getTable(Constant.STOCK_PRODUCT_TABLE);
            string query = Constant.PRODUCT_ID + Constant.EQUAL + Constant.SINGAL_QUOTE + productID + 
                    Constant.SINGAL_QUOTE + Constant.AND + Constant.EXPIRE_DATE + Constant.MORE_THAN +
                    Constant.SINGAL_QUOTE + DateTime.Today + Constant.SINGAL_QUOTE;
            DataRow[] rows = Stock.Select(query);
            if (rows.Count() == Constant.MIN_QUALITY)
            {
                List<string> tempStr = new List<string>();
                //tempStr.Add(Constant.MSG_PRODUCT_NOT_FOUND);
                tempStr.Add(machineID);
                tempStr.Add(type);
                tempStr.Add(Constant.FAIL);
                SerialPortLog.LogOperation("Price Tag Fail");
                SerialPortLog.Log("Price Tag Fail");
                return tempStr;
            }
            else
            {
                DataRow dr = rows.First();
                string name = dr[Constant.NAME].ToString();
                string sellingPrice = dr[Constant.SELLING_PRICE].ToString();
                List<string> output = new List<string>();
                output.Add(machineID);
                output.Add(type);
                output.Add(Constant.SUCCESS);
                output.Add(name);
                output.Add(sellingPrice);
                SerialPortLog.LogOperation("Price Tag Successfully");
                SerialPortLog.Log("Price Tag Successfully");
                /***sound index****/;

                //string output = Constant.PRODUCT_ID + Constant.COLON + Constant.SPACE + productID + Constant.NEWLINE + Constant.NAME + Constant.COLON + Constant.SPACE
                //                + name + Constant.NEWLINE + Constant.SELLING_PRICE + Constant.COLON + Constant.SPACE + Constant.DALLOR +
                //                Math.Round(Convert.ToDecimal(sellingPrice), Constant.TWO) + Constant.NEWLINE /***sound index****/;
                return output;  
            }
        }

        private static List<string> HaveDiscount(string productID, DataRow dr, string machineID, string type)
        {
            try
            {
                string name = dr[Constant.NAME].ToString();
                string quantity = dr[Constant.QUANTITY].ToString();
                double sellingPrice = Convert.ToDouble(dr[Constant.SELLING_PRICE]);
                string freeItermString = Constant.STRING_ZERO;
                string discountString = Constant.STRING_ZERO;

                if (Convert.ToInt32(dr[Constant.FREE_ITEM_QUANTITY]) > Constant.MIN_DISCOUNT)
                {
                     freeItermString = dr[Constant.FREE_ITEM_QUANTITY].ToString();
                    //freeItermString = Constant.NEWLINE + Constant.FREE_ITEM_QUANTITY + Constant.COLON + freeIterm;
                }
                if (Convert.ToInt32(dr[Constant.DISCOUNT]) > Constant.MIN_DISCOUNT)
                {
                    discountString = dr[Constant.DISCOUNT].ToString();
                    //discountString = Constant.NEWLINE + Constant.DISCOUNT + Constant.COLON + Constant.SPACE + discount + Constant.PERCENTAGE;
                }
                double BuPrice = (sellingPrice * Convert.ToInt32(quantity)) / (Convert.ToInt32(quantity) + Convert.ToInt32(freeItermString));
                string finalPrice = (BuPrice * (100 - Convert.ToDouble(discountString)) / 100).ToString();
                finalPrice = (Math.Round(Convert.ToDecimal(finalPrice), Constant.TWO)).ToString();
                List<string> output = new List<string>();
                output.Add(machineID);
                output.Add(type);
                output.Add(Constant.SUCCESS);
                output.Add(name);
                output.Add(finalPrice);
                SerialPortLog.LogOperation("Price Tag Successfully");
                SerialPortLog.Log("Price Tag Successfully");
                /***sound index****/

                //string output = Constant.PRODUCT_ID + Constant.COLON + Constant.SPACE + productID + Constant.NEWLINE + Constant.NAME + Constant.COLON + 
                //                Constant.SPACE + name + Constant.NEWLINE + Constant.SELLING_PRICE + Constant.COLON + Constant.SPACE + Constant.DALLOR +
                //                Math.Round(Convert.ToDecimal(sellingPrice), Constant.TWO) + Constant.NEWLINE + Constant.BUNDLE_UNIT + Constant.COLON +
                //                Constant.SPACE + dr[Constant.BUNDLE_UNIT] + freeItermString + discountString + Constant.NEWLINE/***sound index****/;
                return output;
            }
            catch
            {
                List<string> tempStr = new List<string>();
                tempStr.Add(machineID);
                tempStr.Add(type);
                tempStr.Add(Constant.FAIL);
                SerialPortLog.LogOperation("Price Tag Fail");
                SerialPortLog.Log("Price Tag Fail");
                return tempStr;
            }
        }
        #endregion 


    }
}
